package net.vanchu.mortgagecalculator.mortgage.resultShow;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dtspread.apps.houseloan.R;

import net.vanchu.mortgagecalculator.mortgage.data.monthRepayment.SingleMonthRepay;
import net.vanchu.mortgagecalculator.mortgage.data.repaymentSummary.PrincipalBalancedPart;
import net.vanchu.mortgagecalculator.mortgage.data.repaymentSummary.RepaySummary;
import net.vanchu.mortgagecalculator.mortgage.data.repaymentSummary.TotalBalancedPart;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Granton on 2016/12/23.
 */
class SingleWayResultDataAdapter extends
        RecyclerView.Adapter<SingleWayResultDataAdapter.SingleWayResultDataViewHolder> {

    private ComplainButtonListener complainButtonListener;

    private static final String TAG = SingleWayResultDataAdapter.class.getSimpleName();

    private List<ResultData<RepaySummary.Single>> dataList = new ArrayList<>();

    SingleWayResultDataAdapter(List<ResultData<RepaySummary.Single>> dataList,
                               ComplainButtonListener complainButtonListener) {
        this.dataList = dataList;
        this.complainButtonListener = complainButtonListener;
    }

    @Override
    public SingleWayResultDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return dispatchDataToView(viewType, parent);
    }

    @Override
    public void onBindViewHolder(SingleWayResultDataViewHolder holder, int position) {
        holder.bind(dataList.get(position));
    }

    @Override
    public int getItemViewType(int position) {
        return dataList.get(position).getResultType();
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    private SingleWayResultDataViewHolder dispatchDataToView(int viewType, ViewGroup parent) {
        if (ResultData.ITEM_TYPE_SINGLE_TOTAL_BALANCED_SUM == viewType) {
            return new SingleTotalBalancedSumViewHolder(parent);
        }
        if (ResultData.ITEM_TYPE_SINGLE_PRINCIPAL_BALANCED_SUM == viewType) {
            return new SingleRepaySumPrincipalBalanced(parent);
        }
        if (ResultData.ITEM_TYPE_RESULT_SHOW_COMPLAIN == viewType) {
            return new ComplainEntranceViewHolder(parent);
        }
        if (ResultData.ITEM_TYPE_SINGLE_TABLE_HEADER == viewType) {
            return new SingleWayRepayTableHeader(parent);
        }
        if (ResultData.ITEM_TYPE_SINGLE_TOTAL_BALANCED_TABLE_DATA_ODD_INDEX == viewType
                || ResultData.ITEM_TYPE_SINGLE_PRINCIPAL_BALANCED_TABLE_DATA_ODD_INDEX == viewType) {
            return new SingleWayRepayTableDataOddIndex(parent);
        }
        if (ResultData.ITEM_TYPE_SINGLE_TOTAL_BALANCED_TABLE_DATA_EVEN_INDEX == viewType
                || ResultData.ITEM_TYPE_SINGLE_PRINCIPAL_BALANCED_TABLE_DATA_EVEN_INDEX == viewType) {
            return new SingleWayRepayTableDataEvenIndex(parent);
        }
        if (ResultData.ITEM_TYPE_RESULT_SHOW_FOOTER == viewType) {
            return new ResultShowTableFooter(parent);
        }
        return null;
    }

    abstract class SingleWayResultDataViewHolder extends RecyclerView.ViewHolder {

        SingleWayResultDataViewHolder(ViewGroup parent, int resId) {
            super(LayoutInflater.from(parent.getContext()).inflate(resId, parent, false));
        }

        public abstract void bind(ResultData<RepaySummary.Single> resultData);
    }

    private class ResultShowTableFooter extends SingleWayResultDataViewHolder {

        ResultShowTableFooter(ViewGroup parent) {
            super(parent, R.layout.item_result_show_table_footer);
        }

        @Override
        public void bind(ResultData<RepaySummary.Single> resultData) {

        }
    }

    private class SingleWayRepayTableDataEvenIndex extends SingleWayResultDataViewHolder {

        private final TextView tableDataRemainPrincipal;
        private final TextView tableDataIndexOfMonth;
        private final TextView tableDataTotalToRepay;
        private final TextView tableDataPrincipalToPay;
        private final TextView tableDataInterestToPay;

        SingleWayRepayTableDataEvenIndex(ViewGroup parent) {
            super(parent, R.layout.item_single_way_repay_table_data_even_index);

            tableDataIndexOfMonth = (TextView) itemView.findViewById(R.id.table_data_index_of_month);
            tableDataTotalToRepay = (TextView) itemView.findViewById(R.id.table_data_total_to_repay);
            tableDataPrincipalToPay = (TextView) itemView.findViewById(R.id.table_data_principal_to_pay);
            tableDataInterestToPay = (TextView) itemView.findViewById(R.id.table_data_interest_to_pay);
            tableDataRemainPrincipal = (TextView) itemView.findViewById(R.id.table_data_remain_principal);
        }

        @Override
        public void bind(ResultData<RepaySummary.Single> resultData) {
            TotalBalancedPart.Single totalBalancedPart = resultData.getRepaySummary().getTotalBalancedPart();
            PrincipalBalancedPart.Single principalBalancedPart = resultData.getRepaySummary().getPrincipalBalancedPart();
            List<SingleMonthRepay> singleMonthRepays =
                    resultData.getResultType() == ResultData.ITEM_TYPE_SINGLE_TOTAL_BALANCED_TABLE_DATA_EVEN_INDEX ?
                            totalBalancedPart.getSingleMonthRepays() :
                            principalBalancedPart.getSingleMonthRepays();
            SingleMonthRepay singleMonthRepay = singleMonthRepays.get(getAdapterPosition() - 3);
            tableDataIndexOfMonth.setText(String.valueOf(singleMonthRepay.getIndex()));
            tableDataTotalToRepay.setText(FormatHelper.formatDouble(singleMonthRepay.getTotalRepayNumber()));
            tableDataPrincipalToPay.setText(FormatHelper.formatDouble(singleMonthRepay.getTotalPrincipal()));
            tableDataInterestToPay.setText(FormatHelper.formatDouble(singleMonthRepay.getTotalInterest()));
            int numberOfMonths = resultData.getRepaySummary().getNumberOfMonths();
            double remainPrincipal = singleMonthRepay.isLastMonth(numberOfMonths) ?
                    0 : singleMonthRepay.getRemainPrincipal();
            tableDataRemainPrincipal.setText(FormatHelper.formatDouble(remainPrincipal));
        }
    }

    private class SingleWayRepayTableDataOddIndex extends SingleWayResultDataViewHolder {

        private final TextView tableDataRemainPrincipal;
        private final TextView tableDataIndexOfMonth;
        private final TextView tableDataTotalToRepay;
        private final TextView tableDataPrincipalToPay;
        private final TextView tableDataInterestToPay;

        SingleWayRepayTableDataOddIndex(ViewGroup parent) {
            super(parent, R.layout.item_single_way_repay_table_data_odd_index);

            tableDataIndexOfMonth = (TextView) itemView.findViewById(R.id.table_data_index_of_month);
            tableDataTotalToRepay = (TextView) itemView.findViewById(R.id.table_data_total_to_repay);
            tableDataPrincipalToPay = (TextView) itemView.findViewById(R.id.table_data_principal_to_pay);
            tableDataInterestToPay = (TextView) itemView.findViewById(R.id.table_data_interest_to_pay);
            tableDataRemainPrincipal = (TextView) itemView.findViewById(R.id.table_data_remain_principal);
        }

        @Override
        public void bind(ResultData<RepaySummary.Single> resultData) {
            TotalBalancedPart.Single totalBalancedPart = resultData.getRepaySummary().getTotalBalancedPart();
            PrincipalBalancedPart.Single principalBalancedPart = resultData.getRepaySummary().getPrincipalBalancedPart();
            List<SingleMonthRepay> singleMonthRepays =
                    resultData.getResultType() == ResultData.ITEM_TYPE_SINGLE_TOTAL_BALANCED_TABLE_DATA_ODD_INDEX ?
                            totalBalancedPart.getSingleMonthRepays() :
                            principalBalancedPart.getSingleMonthRepays();
            SingleMonthRepay singleMonthRepay = singleMonthRepays.get(getAdapterPosition() - 3);
            tableDataIndexOfMonth.setText(String.valueOf(singleMonthRepay.getIndex()));
            tableDataTotalToRepay.setText(FormatHelper.formatDouble(singleMonthRepay.getTotalRepayNumber()));
            tableDataPrincipalToPay.setText(FormatHelper.formatDouble(singleMonthRepay.getTotalPrincipal()));
            tableDataInterestToPay.setText(FormatHelper.formatDouble(singleMonthRepay.getTotalInterest()));
            tableDataRemainPrincipal.setText(FormatHelper.formatDouble(singleMonthRepay.getRemainPrincipal()));
        }
    }

    private class SingleWayRepayTableHeader extends SingleWayResultDataViewHolder {

        SingleWayRepayTableHeader(ViewGroup parent) {
            super(parent, R.layout.item_single_way_repay_table_header);
        }

        @Override
        public void bind(ResultData<RepaySummary.Single> resultData) {

        }
    }

    private class ComplainEntranceViewHolder extends SingleWayResultDataViewHolder {

        private final TextView resultShowBtnComplainEntrance;

        ComplainEntranceViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_complain_entrance);
            resultShowBtnComplainEntrance = (TextView) itemView.findViewById(R.id.result_show_btn_complain_entrance);
        }

        @Override
        public void bind(final ResultData<RepaySummary.Single> resultData) {
            resultShowBtnComplainEntrance.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    complainButtonListener.onClickComplainButton();
                }
            });
        }
    }

    private class SingleRepaySumPrincipalBalanced extends SingleWayResultDataViewHolder {

        private final TextView resultSumDecreaseNumber;
        private final TextView resultSumFirstMonthPay;
        private final TextView resultSumInterestPay;
        private final TextView resultSumTotalRepay;

        SingleRepaySumPrincipalBalanced(ViewGroup parent) {
            super(parent, R.layout.item_single_repay_sum_principal_balanced);
            resultSumFirstMonthPay = (TextView) itemView.findViewById(R.id.result_sum_first_month_pay);
            resultSumDecreaseNumber = (TextView) itemView.findViewById(R.id.result_sum_decrease_number);
            resultSumInterestPay = (TextView) itemView.findViewById(R.id.result_sum_interest_pay);
            resultSumTotalRepay = (TextView) itemView.findViewById(R.id.result_sum_total_repay);
        }

        @Override
        public void bind(ResultData<RepaySummary.Single> resultData) {
            PrincipalBalancedPart.Single principalBalancedPart =
                    resultData.getRepaySummary().getPrincipalBalancedPart();
            resultSumFirstMonthPay.setText(FormatHelper.formatDouble(principalBalancedPart.getFirstRepayNumber()));
            resultSumDecreaseNumber.setText(FormatHelper.formatDouble(principalBalancedPart.getDecreaseNumber()));
            resultSumInterestPay.setText(FormatHelper.formatDouble(principalBalancedPart.getPrincipalAndInterest().getInterest()));
            resultSumTotalRepay.setText(FormatHelper.formatDouble(principalBalancedPart.getPrincipalAndInterest().getTotalRepay()));
        }
    }

    private class SingleTotalBalancedSumViewHolder extends SingleWayResultDataViewHolder {

        private final TextView resultSumMonthPay;

        private final TextView resultSumInterestPay;

        private final TextView resultSumTotalRepay;

        SingleTotalBalancedSumViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_single_repay_sum_total_balanced);

            resultSumMonthPay = (TextView) itemView.findViewById(R.id.result_sum_month_pay);
            resultSumInterestPay = (TextView) itemView.findViewById(R.id.result_sum_interest_pay);
            resultSumTotalRepay = (TextView) itemView.findViewById(R.id.result_sum_total_repay);
        }

        @Override
        public void bind(ResultData<RepaySummary.Single> resultData) {
            TotalBalancedPart.Single totalBalancedPart = resultData.getRepaySummary().getTotalBalancedPart();
            double numberRepayPerMonth = totalBalancedPart.getNumberRepayPerMonth();
            double totalRepay = totalBalancedPart.getPrincipalAndInterest().getTotalRepay();
            double interest = totalBalancedPart.getPrincipalAndInterest().getInterest();
            resultSumMonthPay.setText(FormatHelper.formatDouble(numberRepayPerMonth));
            resultSumTotalRepay.setText(FormatHelper.formatDouble(totalRepay));
            resultSumInterestPay.setText(FormatHelper.formatDouble(interest));
        }
    }

}